﻿using AvalonAdmin.BaseViewModel;
using AvalonAdmin.Pages.CarParts.ViewModels.CarParts;
using AvalonAdmin.Pages.CarParts.ViewModels.CarPartsType;
using AvalonAdmin.Pages.CarParts.ViewModels.VehicleSeriesCarPartsType;
using AvalonAdmin.Pages.CarType.ViewModels.Area;
using AvalonAdmin.Pages.CarType.ViewModels.Brand;
using AvalonAdmin.Pages.CarType.ViewModels.CarType;
using AvalonAdmin.Pages.CarType.ViewModels.CarTypeVersion;
using AvalonAdmin.Pages.CarType.ViewModels.Country;
using AvalonAdmin.Pages.CarType.ViewModels.VehicleSeries;
using AvalonAdmin.Pages.Sys.ViewModels;
using Avalonia;
using Avalonia.Controls.Notifications;
using Avalonia.Styling;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using Semi.Avalonia;
using System;
using System.Collections.ObjectModel;
using Notification = Ursa.Controls.Notification;

namespace AvalonAdmin.ViewModels
{
    public partial class MainViewModel : Base
    {
        public MenuViewModel Menus { get; set; } = new MenuViewModel();

        private object? _content;

        public object? Content
        {
            get => _content;
            set => SetProperty(ref _content, value);
        }

        public MainViewModel()
        {
            WeakReferenceMessenger.Default.Register<MainViewModel, string>(this, OnNavigation);
        }


        /// <summary>
        /// 点击菜单跳转
        /// </summary>
        /// <param name="vm"></param>
        /// <param name="s"></param>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        private void OnNavigation(MainViewModel vm, string s)
        {
            Content = s switch
            {
                //MenuKeys.MenuKeyUser => (new UserViewModel(), s),
                MenuKeys.MenuKeyUser => new UserViewModel(),
                MenuKeys.MenuKeyArea => new AreaViewModel(),
                MenuKeys.MenuKeyCountry => new CountryViewModel(),
                MenuKeys.MenuKeyBrand => new BrandViewModel(),
                MenuKeys.MenuKeyVehicleSeries => new VehicleSeriesViewModel(),
                MenuKeys.MenuKeyCarType => new CarTypeViewModel(),
                
                MenuKeys.MenuKeyCarTypeVersion => new CarTypeVersionViewModel(),
                MenuKeys.MenuKeyCarParts => new CarPartsViewModel(),
                MenuKeys.MenuKeyCarPartsType => new CarPartsTypeViewModel(),
                MenuKeys.MenuKeyVehicleSeriesCarPartsType => new VehicleSeriesCarPartsTypeViewModel(),
               //MenuKeys.MenuKeyRuleConfig => "Rule.Views.RuleConfig.Page_RuleConfig";
               //MenuKeys.MenuKeyRuleMapping => "Rule.Views.RuleMapping.Page_RuleMapping_Query";
               //MenuKeys.MenuKeyCarVin => "CarVin.Views.Page_VIN";

                //MenuKeys.MenuKeyProxyCentralControl => new ProxyCentralControlViewModel(),
                //MenuKeys.MenuKeyAgentSetting => new AgentSettingViewModel(),
                //MenuKeys.MenuKeyRouteSetting => new RouteSettingViewModel(),
                MenuKeys.MenuKeyAbout => new AboutViewModel(),
                _ => throw new ArgumentOutOfRangeException(nameof(s), s, null)
            };
        }

        #region 皮肤
        /// <summary>
        /// 皮肤
        /// </summary>
        public ObservableCollection<ThemeItem> Themes { get; } =
        [
            new("Default", ThemeVariant.Default),
            new("Light", ThemeVariant.Light),
            new("Dark", ThemeVariant.Dark),
            new("Aquatic", SemiTheme.Aquatic),
            new("Desert", SemiTheme.Desert),
            new("Dusk", SemiTheme.Dusk),
            new("NightSky", SemiTheme.NightSky)
        ];

        [ObservableProperty] private ThemeItem? _selectedTheme;

        partial void OnSelectedThemeChanged(ThemeItem? oldValue, ThemeItem? newValue)
        {
            if (newValue is null) return;
            var app = Application.Current;
            if (app is not null)
            {
                app.RequestedThemeVariant = newValue.Theme;
                NotificationManager?.Show(
                    new Notification("Theme changed", $"Theme changed to {newValue.Name}"),
                    type: NotificationType.Success,
                    classes: ["Dark"]);
            }
        }

        [ObservableProperty] private string? _footerText = "Settings";

        [ObservableProperty] private bool _isCollapsed;

        partial void OnIsCollapsedChanged(bool value)
        {
            FooterText = value ? null : "Settings";
        }
        #endregion
    }

    public class ThemeItem(string name, ThemeVariant theme)
    {
        public string Name { get; set; } = name;
        public ThemeVariant Theme { get; set; } = theme;
    }
}
